package com.hejia.alauda.service;

import com.hejia.alauda.entity.Channel;
import com.hejia.alauda.entity.Popularize;
import com.hejia.alauda.entity.PopularizeCategory;
import com.hejia.alauda.entity.PopularizePlan;
import com.hejia.alauda.entity.example.*;
import com.hejia.alauda.persistence.mybatis.ChannelMapper;
import com.hejia.alauda.persistence.mybatis.PopularizeCategoryMapper;
import com.hejia.alauda.persistence.mybatis.PopularizeMapper;
import com.hejia.alauda.persistence.mybatis.PopularizePlanMapper;
import com.hejia.alauda.utils.Pager;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PopularizeServiceImpl implements PopularizeService {

	@Resource
	private PopularizeMapper popularizeMapper;

	@Resource
	private PopularizeCategoryMapper popularizeCategoryMapper;

	@Resource
	private PopularizePlanMapper popularizePlanMapper;

	@Resource
	private ChannelMapper channelMapper;

	@Override
	public Pager<Popularize> findPopularizes(String name,
			Boolean isAllPopularizing, Pager<Popularize> pager) {

		PopularizeExample example = new PopularizeExample();
		if (StringUtil.isNotBlank(name)) {
			example.appendCriterion("name like", "%" + name + "%");
		}
		if (isAllPopularizing == null || isAllPopularizing == false) {
			example.appendCriterion("isStopPopularize = 0");
		}

		example.setOrderByClause("createDate desc");

		pager.setList(this.popularizeMapper.selectByExample(example, pager));
		pager.setTotalCount(this.popularizeMapper.countByExample(example));

		return pager;
	}

	@Override
	public Popularize add(Popularize popularize) {

		popularize.setUrl(popularize.getUrl().trim());
		popularize.setCategory(this.popularizeCategoryMapper
				.selectByPrimaryKey(popularize.getCategory().getId()));
		popularize.setCreateDate(new Date());
		popularize.setModifyDate(new Date());

		this.popularizeMapper.insert(popularize);

		popularize.setSourceId(popularize.combineSourceId());
		this.popularizeMapper.updateByPrimaryKey(popularize);

		return popularize;
	}

	@Override
	public Popularize update(Popularize popularize, String operator) {

		Popularize p = this.popularizeMapper.selectByPrimaryKey(popularize
				.getId());

		p.setName(popularize.getName().trim());
		p.setUrl(popularize.getUrl().trim());
		p.setCategory(popularize.getCategory());
		p.setModifyDate(new Date());
		p.setIsStopPopularize(popularize.getIsStopPopularize());
		p.setEditor(operator);

		this.popularizeMapper.updateByPrimaryKey(p);

		return p;
	}

	@Override
	public Popularize findById(long id) {
		return this.popularizeMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deletePopularize(long id) {
		Popularize p = this.popularizeMapper.selectByPrimaryKey(id);
		this.popularizeMapper.deleteByPrimaryKey(p);
	}

	@Override
	public List<Popularize> findPopularizesByExample(PopularizeExample example) {
		if (example == null)
			example = new PopularizeExample();
		return popularizeMapper.selectByExample(example, null);
	}

	@Override
	public Pager<PopularizeCategory> findPopularizeCategorys(String name,
			Pager<PopularizeCategory> pager) {

		PopularizeCategoryExample example = new PopularizeCategoryExample();
		if (StringUtil.isNotBlank(name)) {
			example.appendCriterion("name like", "%" + name + "%");
		}
		example.setOrderByClause("bigger_id desc");
		pager.setList(this.popularizeCategoryMapper.selectByExample(example,
				pager));
		pager.setTotalCount(this.popularizeCategoryMapper
				.countByExample(example));

		return pager;
	}

	@Override
	public List<PopularizeCategory> findTopCategorys() {
		Pager<PopularizeCategory> pager = new Pager<PopularizeCategory>();
		pager.setPageSize(10000);
		PopularizeCategoryExample example = new PopularizeCategoryExample();
		example.appendCriterion("bigger_id is null");
		example.setOrderByClause("createDate desc");
		return this.popularizeCategoryMapper.selectByExample(example, pager);
	}

	@Override
	public PopularizeCategory add(PopularizeCategory category) throws Exception {
		if (this.isExistName(category)) {
			throw new Exception("已存在同名类别");
		}

		category.setCreateDate(new Date());
		category.setModifyDate(new Date());
		this.popularizeCategoryMapper.insert(category);
		return category;
	}

	@Override
	public PopularizeCategory update(PopularizeCategory category)
			throws Exception {
		if (this.isExistName(category)) {
			throw new Exception("已存在同名类别");
		}
		PopularizeCategory c = this.popularizeCategoryMapper
				.selectByPrimaryKey(category.getId());
		c.setName(category.getName());
		c.setIsStopPopularize(category.getIsStopPopularize());
		c.setBigger(category.getBigger());
		c.setModifyDate(new Date());
		this.popularizeCategoryMapper.updateByPrimaryKey(c);
		return c;
	}

	@Override
	public PopularizeCategory findCategoryById(Long id) {
		return this.popularizeCategoryMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean isExistName(PopularizeCategory category) {
		PopularizeCategoryExample example = new PopularizeCategoryExample();
		example.appendCriterion("name =", category.getName());
		if (category.getBigger() != null
				&& category.getBigger().getId() != null) {
			example.appendCriterion("bigger_id =", category.getBigger().getId());
		} else {
			example.appendCriterion("bigger_id is null");
		}
		if (category.getId() != null) {
			example.appendCriterion("id <>", category.getId());
		}

		int count = this.popularizeCategoryMapper.countByExample(example);
		return count > 0;
	}

	@Override
	public void deletePopularizeCategory(long id) throws Exception {
		if (isExistSmallerCategory(id)) {
			throw new Exception("存在稍小推广类别，不能删除");
		}
		PopularizeCategory category = this.popularizeCategoryMapper
				.selectByPrimaryKey(id);
		this.popularizeCategoryMapper.deleteByPrimaryKey(category);
	}

	@Override
	public boolean isExistSmallerCategory(long id) {
		PopularizeCategoryExample example = new PopularizeCategoryExample();
		example.appendCriterion("bigger_id =", id);
		int count = this.popularizeCategoryMapper.countByExample(example);
		return count > 0;
	}

	@Override
	public List<PopularizeCategory> findSmallerCategorys() {

		Pager<PopularizeCategory> pager = new Pager<PopularizeCategory>();
		pager.setPageSize(10000);
		PopularizeCategoryExample example = new PopularizeCategoryExample();
		example.appendCriterion("bigger_id is not null");
		example.setOrderByClause("bigger_id desc");

		return this.popularizeCategoryMapper.selectByExample(example, pager);
	}

	@Override
	public boolean isUsedByPopularize(long categoryId) {
		PopularizeExample example = new PopularizeExample();
		example.appendCriterion("category_id=", categoryId);
		int count = popularizeMapper.countByExample(example);
		return count > 0;
	}

	@Override
	public PopularizePlan add(PopularizePlan plan) {
		plan.setCreateDate(new Date());
		plan.setModifyDate(new Date());
		this.popularizePlanMapper.insert(plan);
		return this.popularizePlanMapper.selectByPrimaryKey(plan.getId());
	}

	@Override
	public List<PopularizePlan> findPlansByPopularizeId(Long popularizeId) {
		Pager<PopularizePlan> pager = new Pager<PopularizePlan>();
		pager.setPageSize(10000);
		PopularizePlanExample example = new PopularizePlanExample();
		example.appendCriterion("popularize_id =", popularizeId);
		example.setOrderByClause("createDate desc");
		return this.popularizePlanMapper.selectByExample(example, pager);
	}

	@Override
	public void deletePopularizePlan(long id) {
		PopularizePlan plan = this.popularizePlanMapper.selectByPrimaryKey(id);
		this.popularizePlanMapper.deleteByPrimaryKey(plan);
	}

	@Override
	public Pager<Channel> findChannelsByKeyword(String keyword,
			Pager<Channel> pager) {

		ChannelExample example = new ChannelExample();
		if (StringUtils.isNotBlank(keyword)) {
			example.appendCriterion("name like", "%" + keyword + "%");
			Example.Criteria criteria = example.createCriteria();
			criteria.addCriterion("number like", "%" + keyword + "%");
			example.or(criteria);
		}
		example.setOrderByClause("createDate desc");

		pager.setList(channelMapper.selectByExample(example, pager));
		pager.setTotalCount(channelMapper.countByExample(example));

		return pager;
	}

	@Override
	public Channel addChannel(Channel channel) {

		channel.setCreateDate(new Date());
		channel.setModifyDate(new Date());
		this.channelMapper.insert(channel);

		return channel;
	}

	@Override
	public boolean isExistChannel(Channel channel) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", channel.getName());
		map.put("number", channel.getNumber());
		if (channel.getId() != null) {
			map.put("id", channel.getId());
		}
		int count = this.channelMapper.countChannel(map);

		return count > 0;
	}

	@Override
	public void deleteChannel(Long id) {
		Channel channel = this.channelMapper.selectByPrimaryKey(id);
		this.channelMapper.deleteByPrimaryKey(channel);
	}

	@Override
	public Channel updateChannel(Channel channel) {
		this.channelMapper.updateByPrimaryKey(channel);
		return channel;
	}

	@Override
	public Channel findChannelById(Long id) {
		return this.channelMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<String> findSourceIdByBiggerCategoryId(Long biggerCategoryId) {
		return this.popularizeMapper
				.findSourceIdByBiggerCategoryId(biggerCategoryId);
	}

	@Override
	public List<String> findSourceIdByCategoryId(Long categoryId) {
		return this.popularizeMapper.findSourceIdByCategoryId(categoryId);
	}

	@Override
	public List<String> findSourceIdByPopularizeId(Long popularizeId) {
		return this.popularizeMapper.findSourceIdByPopularizeId(popularizeId);
	}
}
